<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
/**
 * 后台管理Home
 * @package admin
 */
class AdmincpController extends AdminAction
{
    

    public function init()
    {
        // $this->_config = Registry::get ('config');
    }


    /**
     * 加载框架页
     */
    public function indexAction()
    {
        //加载默认视图
    }


    /**
     * 加载框架页的左边页
     * @author RD
     */
    public function leftAction ()
    {
    	$loginfo = AdminUser::getLoginInfo();
    	$gid = $loginfo['gid'];
    	
        require_once APP . '/application/configs/adminleft.php';
        $m = 'menu_' . $gid;
        $this->view->cadata       = $$m;
    }


    /**
     * 加载框架页的主框架页
     */
    public function mainAction ()
    {
        // default view
    }


    /**
     * 后台登陆,不需验证
     * @author RD
     */
    public function loginAction ()
    {
        $username = $this->getPost ('username', '');
        $password = $this->getPost ('password', '');
        $authcode = $this->getPost ('authcode', '');
        if (!empty ($username) && !empty ($password) && !empty ($authcode)) {

            if (empty ($_SESSION['authcode']) || strtolower($_SESSION['authcode']) != strtolower ($authcode)) {
                $this->view->message = '验证码不正确';
                $this->view->username = $username;
                $this->view->password = $password;
            } else {
                require_once APP . '/application/models/AdminUser.php';
                $admin = new AdminUser ($this->db);
                if ($admin->login ($username, $password)) {
                    unset ($_SESSION['authcode']);
                    $this->_redirect ($this->url($this->view->fullUrl, "admincp", "index"));
                } else {
                    $this->view->message = '帐号密码不正确';
                    $this->view->username = $username;
                }
            }
        }
    }


    /**
     * 显示验证码,不需验证
     * @author RD
     */
    public function authcodeAction ()
    {
        $this->setAutoRender (false);

        require_once APP . '/application/models/Util.php';
        $rstr = array ('a','b','c','d','e','f','g','h','j','k','m','n','o','p','r','s','t','u','v','w','x','y','z',
                       'A','B','C','D','E','F','G','H','J','K','M','N','O','P','R','S','T','U','V','W','X','Y','Z');

        //验证码
        $authcode = $_SESSION['authcode'] = $rstr[array_rand ($rstr, 1)] . $rstr[array_rand ($rstr, 1)].$rstr[array_rand ($rstr, 1)]. $rstr[array_rand ($rstr, 1)];

        //输出图片
        /**
        Header ('Expires', '-1', true);
        Header ('Cache-Control', 'no-store, private, post-check=0, pre-check=0, max-age=0', true);
        Header ('Pragma', 'no-cache', true);
        Header ('Content-type', 'image/jpeg', true);
        **/
        $im  = Util::createJpeg ($authcode, APP . '/application/configs/COPRGTB.TTF');
        imageJpeg ($im);
        imagedestroy ($im);
    }


    /**
     * 注销用户，切换用户名
     * @author RD
     */
    public function cancelAction ()
    {
        require_once APP . '/application/models/AdminUser.php';
        AdminUser::loginOut();
        $this->_redirect ($this->url($this->view->baseUrl,'admincp','login'));
    }


    /**
     * 退出后台管理
     * @author RD
     */
    public function exitAction ()
    {
        require_once APP . '/application/models/AdminUser.php';
        AdminUser::loginOut();
        unset ($_SESSION);
        exit ('<script>window.opener="";window.close();</script>');
    }


    /**
     * 更新缓存
     * @author RD
     */
    public function updatecacheAction () {

    }
}
